package com.bs.demo.controller;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
//import com.bs.demo.tasks.DynamicTask;
import com.alibaba.fastjson.JSONObject;
import com.bs.common.utils.Result;
import com.bs.common.utils.SnowFlakeUtil;
import com.bs.demo.feign.AuthFeign;
import com.bs.demo.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author bingshao
 * @date 2023/2/15
 **/
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private AuthFeign authFeign;

//    @Autowired
//    private DynamicTask task;
//
//    @RequestMapping("start")
//    public void startTask() throws ParseException {
//        task.startCron();
//    }
//
//    // 测试访问： http://localhost:8080/stopById?taskId=任务一
//    @RequestMapping("stopById")
//    public void stopById(String taskId) {
//        task.stop(taskId);
//    }
//
//    @RequestMapping("stopAll")
//    public void stopAll() {
//        task.stopAll();
//    }


//    public static void main(String[] args) {
////        HttpResponse httpResponse = HttpUtil
////                .createGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd75f9fcf56fa150c&secret=d6df8b6f4789122fd8233a4f04b43230&code=031nA4ll2xfbPa4GLyll2eTKfz0nA4lF&grant_type=authorization_code")
////                .execute();
////        String body = Optional.ofNullable(httpResponse.body()).orElse("{}");
//
//        HttpResponse httpResponse = HttpUtil
//                .createGet("https://api.weixin.qq.com/sns/userinfo?access_token=66_5IQdklejacInpwOyW9IDCYjZFPFOUPNpf73Kfu3I5Qn-xeDbSlGccIB8KW8PUroidRsubIltPM5jtsrvnxHEpRwGInfPGiqsODD0OkVJ-UE&openid=orwEw5qjR8UeMpWHnWX9Ec0dAU0o&lang=zh_CN")
//                .execute();
//        String body = Optional.ofNullable(httpResponse.body()).orElse("{}");
//    }

//    public static void main(String[] args) {

    // 获取token
//        HttpResponse httpResponse = HttpUtil
//                .createGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd75f9fcf56fa150c&secret=d6df8b6f4789122fd8233a4f04b43230")
//                .execute();
////
////        //66_aEhUiWJzrrqTFfs0Zvn9BmH_3mbF4lwWGVdhYRdlzC8l9M_U-X4ZWZoXoe-ErD3Bq2mdN_V6Qs_wbOxrs72Mswcr7WPapQu9LNVX_0-NwlGZ5_R9Y3BRjRQwGUARCHfAFAKVA
//        String body = Optional.ofNullable(httpResponse.body()).orElse("{}");

    // 获取jsapi_ticket
//        HttpResponse httpResponse = HttpUtil
//                .createGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=66_aEhUiWJzrrqTFfs0Zvn9BmH_3mbF4lwWGVdhYRdlzC8l9M_U-X4ZWZoXoe-ErD3Bq2mdN_V6Qs_wbOxrs72Mswcr7WPapQu9LNVX_0-NwlGZ5_R9Y3BRjRQwGUARCHfAFAKVA&type=jsapi")
//                .execute();
//        // LIKLckvwlJT9cWIhEQTwfKU4VLDYhkgg9vpyNVJChehSWteA_OIoO_DPv-LBkwu4J-KJyMKaPg7nhf4x4iFUWQ
//        String body = Optional.ofNullable(httpResponse.body()).orElse("{}");

    // 加密获取签名
//        String s = "jsapi_ticket=LIKLckvwlJT9cWIhEQTwfKU4VLDYhkgg9vpyNVJChehSWteA_OIoO_DPv-LBkwu4J-KJyMKaPg7nhf4x4iFUWQ&noncestr=43312e49-891d-4344-8e67-7f6c5e94d38b&timestamp=1677047693930&url=http://192.168.78.154:5273/h5/";
//
//        String s2 = DigestUtil.sha1Hex(s);
//        System.out.println(s2);
//    }

    public static void main(String[] args) {
//        String encode = URLEncoder.encode("https://bingshao.cool");
//        System.out.println(encode);

//        JSONObject obj = new JSONObject();
//        System.out.println(obj.isEmpty()); //true
//        obj.put("name", "bingshao");
//        System.out.println(obj.isEmpty()); //false
//        System.out.println("asddffa".indexOf("a"));

//        System.out.println(SnowFlakeUtil.getId());
//        System.out.println(Integer.valueOf("05"));
//
//        System.out.println(fillString(1, 2));
//        System.out.println(fillString(12, 2));

        List<StudentVo> studentVoList = new ArrayList<>();
        StudentVo studentVo1 = new StudentVo();
        StudentVo studentVo2 = new StudentVo();
        studentVoList.add(studentVo1);
        studentVoList.add(studentVo2);
        List<String> idList = studentVoList.stream().map(StudentVo::getId).map(String::valueOf).collect(Collectors.toList());
    }

    public static String fillString(int num, int digit) {
        return String.format("%0" + digit + "d", num);
    }


    @GetMapping("/getInt")
    public Result<Integer> test() throws Exception {
        Result<Integer> integerResult = authFeign.testA();
        if (integerResult.getCode() != 200) {
            throw new Exception(integerResult.getMessage());
        }
        Integer data = integerResult.getData();
        System.out.println(data);
        return Result.success(data);
    }
}


